<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed httpFetch');
/**
 *
 * ClassName: httpFetch
 *
 * description...
 *
 * @author wanghaifei <wanghaifei@soften.cn>
 *
 */
class Http_fetch{

    /**
    * xxxxxxx
    *
    * @access public
    * @param string $charset xxxxx [Must]
    * @param array $contentType xxxxxx [Optional]
    * @param int $display xxxxxxxxx [Optional]
    * @param bool $display xxxxxxxxx [Optional]
    * @return string $mixed
    */

    var $htmlcontent;

	public function getHtml($url){

		$content = byCurlRequest($url);

	    return $content;
	}


    public function get($start_str,$end_str){

		$start_str = preg_quote($start_str,'/');
        $end_str = preg_quote($end_str,'/');

	    if(preg_match_all('/'.$start_str.'(.*?)'.$end_str.'/ims', $this->htmlcontent, $matches)) 
		{
		
            return $matches[1];		   
		
		}else{
		
		    return false;
		}
	
	}

	/*
     * 获取标签内的属性和内存
     * $tag 

	*/

	public function getAttr($tag,$end='>',$content=false)
	{   
		if(empty($tag)) return false;

		if(empty($end))	$end = '>';

		$end_str = preg_quote($end,'/');

	    if(!strpos($tag,'<')) 
			
		$start_str = '<'.$tag;

		$start_preg = preg_quote($start_str,'/');

	    if(preg_match_all('/'.$start_preg.'(.*?)'.$end_str.'/ims', $this->htmlcontent, $matches)) 
		{

            $attrOneVal = $matches[0];


			foreach($attrOneVal as $val)
			{	
				//标签之内的内容
                if($content)
			    {
					$sContent = preg_quote($val,'/');
					$eContent ='\\<(?:\\\\)*?\\/'.$tag.'\\>';
				    if(preg_match('/'.$sContent.'(.*?)'.$eContent.'/ims', $this->htmlcontent, $matches))
					{
					    $attrList['content'] = $matches[1];
					}
				}

                //过滤头尾
                $val = str_replace($start_str,'',$val);
                $endPos = strrchr($val,'"')? strrchr($val,'"'):strrchr($val,'\'');
				$endPos = substr($endPos,1);
				$val = trim(str_replace($endPos,'',$val));

			    //如果存在属性
                if(strpos($val,'=')){

                    $astr = "\"";
				    $bstr = "'";
				    $astrpos = strpos($val,$astr);
				    $bstrpos = strpos($val,$bstr);

				    if($astrpos&&$bstrpos)
				    {
				        if($astrpos>$bstrpos)
				        {
					        $expStr = $bstr;
					    }else{
					
					       $expStr = $astr;
					    }
				    }else{
				
                        $expStr = $astrpos?$astr:$bstr;
				    }

				    $expList = explode($expStr.' ',$val);


				    foreach($expList as $attr)
				    {
				    	$attr = str_replace("\\".$expStr,'',$attr);

					    $attr = str_replace($expStr,'',$attr);

				    	$attr = str_replace("\\/",'/',$attr);

				        list($key,$value) = explode('=',$attr,2);

				    	$endstr = substr($value,-1);

					    if($endstr=='\\')
					    {
					        $value = substr($value,0,-1);
					    }
					    $attrList['attr'][$key] = $value;

				    }

				}//if
	

				$allList[] = $attrList;
				$attrList = array();
			}


            return $allList;
		}else{
		
		    return false;
		}
		

	}

	/*
     * 获取标签里边的内容
     * $tag 
	*/

	public function tagContent($tag)
	{   
		if(empty($tag)) return false;

	    if(!strpos($tag,'<')) 
			
		$start_str = '<'.$tag;

		$start_preg = preg_quote($start_str,'/');

	    if(preg_match_all('/'.$start_preg.'.*?\>(.*?)'.'\<\/|\\\\\/'.$tag.'\>/ims', $this->htmlcontent, $matches)) 
		{   
			return $matches[1];
	    }else{
		
		    return false;
		}
	}

}
